<template>
  <div class="content">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>条件查询</span>
      </div>
      <!-- 搜索输入框 -->
      <el-row>
        <el-col :span="6">
          <div class="input-holder">
            <span>标题</span>
            <el-input
              v-model="articleSearch.title"
              placeholder="请输入标题"
              clearable
            />
          </div>
        </el-col>
        <el-col :span="6">
          <div class="input-holder">
            <span>作者</span>
            <el-input
              v-model="articleSearch.author"
              placeholder="请输入作者名"
              clearable
            />
          </div>
        </el-col>
        <el-col :span="6">
          <span>编辑类型</span>
          <el-select v-model="articleSearch.editorType" placeholder="请选择" @change="selectChange">
            <el-option
              v-for="item in articleOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-col>
      </el-row>
      <el-row>
        <!-- 搜索按钮 -->
        <el-col :offset="18" :span="6">
          <el-button size="default" @click="resetSearch">重置</el-button>
          <el-button type="primary" size="default" @click="search">搜索</el-button>
        </el-col>
      </el-row>
    </el-card>

    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <!-- 新增文章按钮 -->
      <div slot="header">
        <el-button
          v-btn-permi:add
          type="primary"
          size="default"
          @click="addArticle"
        >新增</el-button>
      </div>

      <div class="table-holder">
        <!-- 文章展示表格 -->
        <el-table :data="articleList" border style="width: 100%">
          <el-table-column fixed="left" type="index" label="#" width="50" />
          <el-table-column prop="title" label="文章标题" width="200" />
          <el-table-column label="展示图片" width="200">
            <template slot-scope="scope">
              <img class="image" :src="scope.row.coverImg" alt="">
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="添加时间" width="250" />
          <el-table-column prop="author" label="文章作者" width="200" />
          <el-table-column label="文章是否显示" width="200">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.isShow"
                style="display: block"
                :active-value="1"
                :inactive-value="0"
                @change="switchChange(scope.row)"
              />
            </template>
          </el-table-column>
          <el-table-column label="内容" width="200">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.editorType === 1">MarkDown</el-tag>
              <el-tag v-else>富文本框</el-tag>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="scope">
              <el-button
                v-btn-permi:edit
                type="text"
                size="small"
                icon="el-icon-view"
                @click="edit(scope.row.id)"
              >编辑文章</el-button>
              <el-button
                v-btn-permi:remove
                type="text"
                style="color: red"
                size="small"
                @click="remove(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页器 -->
      <div class="pagination-holder">
        <el-pagination
          :current-page="page.start"
          :page-sizes="page.pageSize"
          :page-size="page.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  requestArticleList,
  deleteArticle,
  changeShowStatus
} from '@/api/content'
import mixin from '@/mixins/index'
export default {
  mixins: [mixin],
  data() {
    return {
      articleType: '',
      // 文章列表
      articleList: [],
      // 文章搜索内容
      articleSearch: {
        title: '',
        author: '',
        editorType: ''
      },
      // 搜索选择文章内容种类
      articleOptions: [{
        value: '0',
        label: '富文本'
      },
      {
        value: '1',
        label: 'markdown'
      }]
    }
  },
  created() {
    this.getArticleList()
  },
  methods: {
    // 重置搜索结果
    resetSearch() {
      this.articleSearch = {
        title: '',
        author: '',
        editorType: ''
      }
      this.getArticleList()
    },
    // 搜索
    search() {
      this.page.start = 0
      this.getArticleList()
    },
    // 获取文章列表信息
    getArticleList() {
      requestArticleList(this.page, this.removeBlock(this.articleSearch)).then((res) => {
        this.page.total = res.data.total
        this.articleList = res.data.rows
        // console.log(res)
      })
    },
    // 跳转到添加页面
    addArticle() {
      this.$router.push('/content/add-article')
    },
    // 改变每页数量
    handleSizeChange(val) {
      this.page.limit = val
      this.getArticleList()
    },
    // 改变页码
    handleCurrentChange(val) {
      this.page.start = val
      this.getArticleList()
    },
    // 改变文章显示状态
    switchChange(item) {
      console.log(item)
      changeShowStatus({ id: item.id, isShow: item.isShow }).then((res) => {
        console.log(res)
        if (res.code === 20000) {
          this.$message({
            type: 'success',
            message: '改变状态成功!'
          })
        } else {
          this.$message({
            type: 'fail',
            message: '改变状态失败'
          })
          item.isShow = item.isShow === 0 ? 1 : 0
        }
      })
    },
    // 改变搜索框选项
    selectChange(e) {
      console.log(e)
    },
    // 编辑
    edit(id) {
      this.$router.push({ name: 'EditArticle', params: { id }})
    },
    // 删除
    remove(id) {
      console.log(id)
      const _this = this
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteArticle(id).then((res) => {
            // console.log(res);
            if (res.code === 20000) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              _this.getArticleList()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

@import '@/style/index.scss'

<style lang="scss" scoped>
.content {
  .image {
    width: 80%;
    height: 80%;
  }

  .input-holder{
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 80%;

    span{
      width: 50px;
    }
  }
}
</style>
